/**
 * 
 */
package com.carsa.blackberry.util;

import java.io.PrintStream;

/**
 * @author Nahuel Barrios.
 * @since 03/08/2010.
 * @version 1.0.
 */
public class Logger {

    private static final int    LOG_TYPE_ERROR = 0;
    private static final int    LOG_TYPE_INFO  = 1;
    private static final int    LOG_TYPE_DEBUG = 2;

    private static final String ERROR          = "ERROR:\t";
    private static final String INFO           = "INFO:\t";
    private static final String DEBUG          = "DEBUG:\t";
    private static final String MESSAGE        = "\tMesage:\t";

    /**
     * @param anObject
     *            La clase donde ocurrio el error.
     * @param methodName
     *            El nombre del metodo en el cual se produjo el error.
     * @return
     */
    public static String getLocationMessageFor(Object anObject, String methodName) {
	String message = "";
	if (anObject != null)
	    message += anObject.getClass().getName();
	else
	    message += "- undefined -";

	message += ".";

	if (methodName != null)
	    message += methodName;
	else
	    message += "- undefined -";

	return "Ocurrio un error en: " + message;
    }

    /**
     * @param descript
     *            La descripcion del error ocurrido.
     */
    public static void logError(String descript) {
	loguear(descript, null, LOG_TYPE_ERROR);
    }

    /**
     * @param descript
     *            A simple (Generic) descript of the error.
     * @param anException
     *            The catched exception.
     */
    public static void logError(Exception anException) {
	loguear(null, anException, LOG_TYPE_ERROR);
    }

    /**
     * @param descript
     *            A simple (Generic) descript of the error.
     * @param anException
     *            The catched exception.
     */
    public static void logError(String descript, Exception anException) {
	loguear(descript, anException, LOG_TYPE_ERROR);
    }

    /**
     * @param descript
     *            A simple (generic) descript of the error.
     * @param exceptionMessage
     *            A detailed descript of the error, like the message of an exception.
     */
    public static void logError(String descript, String exceptionMessage) {
	loguear(descript, new Exception(exceptionMessage), LOG_TYPE_ERROR);
    }

    /**
     * @param info
     *            The info to log.
     */
    public static void logInfo(String info) {
	loguear(info, null, LOG_TYPE_INFO);
    }

    /**
     * @param descript
     */
    public static void logDebug(String descript) {
	loguear(descript, null, LOG_TYPE_DEBUG);
    }

    /**
     * This function is the only that have to print the messages.
     * 
     * @param descript
     *            A simple message to log.
     * @param anException
     *            An exception to log.
     * @param logType
     *            The method of logging. It has to be one of ERROR, DEBUG or INFO.
     */
    private static void loguear(String descript, Exception anException, int logType) {

	PrintStream printStream = null;
	switch (logType) {
	case LOG_TYPE_ERROR:
	    printStream = System.err;
	    printStream.println(ERROR + descript == null ? "" : descript);
	    if (anException != null)
		printStream.println(MESSAGE + anException.getMessage());
	    printStream.println("--------------------");
	    return;
	default:
	    printStream = System.out;
	    break;
	}

	switch (logType) {
	case LOG_TYPE_INFO:
	    printStream.println(INFO + descript);
	    break;
	case LOG_TYPE_DEBUG:
	    printStream.println(DEBUG + descript);
	    break;
	}

	return;
    }

}
